#include <stdio.h>
#include <stdlib.h>

int main(){
    char buf[] = "hello_world!\n";
    FILE*fp = NULL;
    int size;
    /*打开文件*/
    if(NULL== (fp=fopen("./test_file","w+"))){
        perror("open ERROR");
        goto err;
    }
    printf("打开文件成功\n");

    /*写入数据*/
    if(sizeof(buf)>fwrite(buf,1,sizeof(buf),fp)){
        printf("fwrite error\n");
        perror("fwrite error");
        goto err;
        fclose(fp);
    }
    printf("文件写入成功！\n");

    /*读数据*/
    if(12>(size = fread(buf,1,12,fp))){
        if(ferror(fp)){
            printf("fread error\n");
            fclose(fp);
            goto err;
       }
    }
    printf("读取成功！\n");
    printf("buf[]= %s",buf);
    fclose(fp);
    exit(0);

    err:
    exit(-1);
}